package com.cn.xiaonuo.tcrfinance.tcrshops.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.xiaonuo.common.constant.Status;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.utils.DateUtils;
import com.cn.xiaonuo.common.utils.ExportExcelUtils;
import com.cn.xiaonuo.common.utils.StringUtils;
import com.cn.xiaonuo.common.vo.ExcelData;
import com.cn.xiaonuo.tcrfinance.tcrorder.entity.StorageRecord;
import com.cn.xiaonuo.tcrfinance.tcrorder.mapper.StorageRecordMapper;
import com.cn.xiaonuo.tcrfinance.tcrshops.entity.PvShop;
import com.cn.xiaonuo.tcrfinance.tcrshops.entity.ThirdStorage;
import com.cn.xiaonuo.tcrfinance.tcrshops.mapper.PvShopMapper;
import com.cn.xiaonuo.tcrfinance.tcrshops.mapper.ThirdStorageMapper;
import com.cn.xiaonuo.tcrfinance.tcrshops.param.*;
import com.cn.xiaonuo.tcrfinance.tcrshops.service.ThirdStorageService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: slb-reports-java
 * @Author: likui
 * @CreateTime: 2023-01-31  09:21
 */
@Service
public class ThirdStorageServiceImpl implements ThirdStorageService {
    @Autowired
    private ThirdStorageMapper thirdStorageMapper;
    @Autowired
    private PvShopMapper pvShopMapper;
    @Autowired
    private StorageRecordMapper storageRecordMapper;


    @Override
    public BusinessMessage pageThirdStorage(PageThirdStorageParam param) {
        BusinessMessage message = new BusinessMessage();
        QueryWrapper<ThirdStorage> queryWrapper = new QueryWrapper<>();
        if (ObjectUtils.isNotEmpty(param.getStorageId())) {
            queryWrapper.eq("id", param.getStorageId());
        }
        if (StringUtils.isNotBlank(param.getStorageName())) {
            queryWrapper.likeRight("storage_name", param.getStorageName());
        }
        if (ObjectUtils.isNotEmpty(param.getShopId())) {
            queryWrapper.eq("shop_id", param.getShopId());
        }
        queryWrapper.eq("deleted", 0);
        Page<ThirdStorage> thirdStoragePage = thirdStorageMapper.selectPage(new Page<>(param.getPageNo(), param.getPageSize()), queryWrapper);
        message.setData(thirdStoragePage);
        message.setCode(Status.successCode);
        message.setSuccess(true);
        message.setMessage(Status.success);
        return message;
    }

    @Override
    public BusinessMessage addThirdStorage(AddThirdStorageParam param) {
        BusinessMessage message = new BusinessMessage();
        if (ObjectUtils.isEmpty(param.getShopId())) {
            message.setCode(Status.failCode);
            message.setSuccess(false);
            message.setMessage("参数不完整!");
            return message;
        }
        PvShop pvShop = pvShopMapper.selectById(param.getShopId());
        ThirdStorage thirdStorage = new ThirdStorage();
        BeanUtils.copyProperties(param, thirdStorage);
        thirdStorage.setShopName(pvShop.getShopName());
        thirdStorageMapper.insert(thirdStorage);
        message.setCode(Status.successCode);
        message.setSuccess(true);
        message.setMessage(Status.success);
        return message;
    }

    @Override
    public BusinessMessage editThirdStorage(EditThirdStorageParam param) {
        BusinessMessage message = new BusinessMessage();
        if (ObjectUtils.isEmpty(param.getStorageId())){
            message.setCode(Status.failCode);
            message.setSuccess(false);
            message.setMessage("参数不完整!");
            return message;
        }
        ThirdStorage selectById = thirdStorageMapper.selectById(param.getStorageId());
        if (ObjectUtils.isNotEmpty(selectById)){
            if (ObjectUtils.isEmpty(param.getDeleted())) {
                if (ObjectUtils.isEmpty(param.getShopId())) {
                    message.setCode(Status.failCode);
                    message.setSuccess(false);
                    message.setMessage("参数不完整!");
                    return message;
                }
                PvShop pvShop = pvShopMapper.selectById(param.getShopId());
                BeanUtils.copyProperties(param, selectById);
                selectById.setShopName(pvShop.getShopName());
            }else {
                selectById.setDeleted(param.getDeleted());
            }
            thirdStorageMapper.updateById(selectById);
        }
        message.setCode(Status.successCode);
        message.setSuccess(true);
        message.setMessage(Status.success);
        return message;
    }

    @Override
    public BusinessMessage storageRecordPage(StorageRecordPageParam param) {
        BusinessMessage message = new BusinessMessage();
        if (ObjectUtils.isEmpty(param.getRecordType())){
            message.setCode(Status.failCode);
            message.setSuccess(false);
            message.setMessage("参数不完整!");
            return message;
        }
        QueryWrapper<StorageRecord> queryWrapper = this.queryWrapper(param);
        Page<StorageRecord> recordPage = storageRecordMapper.selectPage(new Page<>(param.getPageNo(), param.getPageSize()), queryWrapper);
        message.setData(recordPage);
        message.setCode(Status.successCode);
        message.setSuccess(true);
        message.setMessage(Status.success);
        return message;
    }

    public QueryWrapper<StorageRecord> queryWrapper(StorageRecordPageParam param){
        QueryWrapper<StorageRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_type",param.getRecordType());
        queryWrapper.eq("deleted",0);
        if (ObjectUtils.isNotEmpty(param.getOrderNo())){
            queryWrapper.eq("order_no",param.getOrderNo());
        }
        if (ObjectUtils.isNotEmpty(param.getBeginInTime()) && ObjectUtils.isNotEmpty(param.getEndInTime())){
            queryWrapper.ge("in_time",param.getBeginInTime());
            queryWrapper.le("in_time",param.getEndInTime());
        }
        if (ObjectUtils.isNotEmpty(param.getBeginOutTime()) && ObjectUtils.isNotEmpty(param.getEndOutTime())){
            queryWrapper.ge("out_time",param.getBeginOutTime());
            queryWrapper.le("out_time",param.getEndOutTime());
        }
        if (ObjectUtils.isNotEmpty(param.getGoodsName())){
            queryWrapper.likeRight("goods_name",param.getGoodsName());
        }
        if (ObjectUtils.isNotEmpty(param.getShopId())){
            queryWrapper.likeRight("shop_id",param.getShopId());
        }
        if (ObjectUtils.isNotEmpty(param.getShopName())){
            queryWrapper.likeRight("shop_name",param.getShopName());
        }
        if (ObjectUtils.isNotEmpty(param.getAccount())){
            queryWrapper.likeRight("account",param.getAccount());
        }
        if (ObjectUtils.isNotEmpty(param.getRealName())){
            queryWrapper.likeRight("real_name",param.getRealName());
        }
        if (ObjectUtils.isNotEmpty(param.getOutType())){
            queryWrapper.likeRight("out_type",param.getOutType());
        }
        queryWrapper.orderByDesc("created_time");
        return queryWrapper;
    }

    @Override
    public BusinessMessage updateVoucher(UpdateVoucherParam param) {
        BusinessMessage message = new BusinessMessage();
        if (ObjectUtils.isEmpty(param.getId())){
            message.setCode(Status.failCode);
            message.setSuccess(false);
            message.setMessage("参数不完整!");
            return message;
        }
        StorageRecord storageRecord = storageRecordMapper.selectById(param.getId());
        if (ObjectUtils.isEmpty(storageRecord)){
            message.setCode(Status.failCode);
            message.setSuccess(false);
            message.setMessage("数据信息不存在!");
            return message;
        }
        storageRecord.setCert(param.getCert());
        storageRecord.setUpdatedTime(new Date());
        storageRecordMapper.updateById(storageRecord);
        message.setCode(Status.successCode);
        message.setSuccess(true);
        message.setMessage(Status.success);
        return message;
    }

    @Override
    public void outgoingRecordExcel(HttpServletResponse response, StorageRecordPageParam param) {
        if (ObjectUtils.isEmpty(param.getRecordType())){
            return;
        }
        QueryWrapper<StorageRecord> queryWrapper = this.queryWrapper(param);
        List<StorageRecord> storageRecords = storageRecordMapper.selectList(queryWrapper);
        try {
            List<List<Object>> rows = new ArrayList();
            ExcelData data = new ExcelData();
            data.setName("出仓记录导出");//sheet表名称
            //表头
            List<String> titles = new ArrayList();
            titles.add("订单编号");
            titles.add("出仓时间（发货时间）");
            titles.add("出仓类型");
            titles.add("商品名称");
            titles.add("所属商家");
            titles.add("数量");
            titles.add("账户");
            titles.add("真实姓名");
            data.setTitles(titles);
            for (StorageRecord storageRecord : storageRecords) {
                List<Object> row1 = new ArrayList();
                row1.add(storageRecord.getOrderNo());
                if (ObjectUtils.isNotEmpty(storageRecord.getOutTime())){
                    String format = DateUtils.yMdHmsS.format(storageRecord.getOutTime());
                    row1.add(format);
                }else {
                    row1.add("");
                }

                if(storageRecord.getOutType()!=null){
                    if (storageRecord.getOutType().equals(0)) {
                        row1.add("提货");
                    } else if (storageRecord.getOutType().equals(1)){
                        row1.add("转仓");
                    }
                }
                row1.add(storageRecord.getGoodsName());
                row1.add(storageRecord.getShopName());
                row1.add(storageRecord.getCount());
                row1.add(storageRecord.getAccount());
                row1.add(storageRecord.getRealName());
                rows.add(row1);
            }

            data.setRows(rows);
            ExportExcelUtils.exportExcel(response, "出仓记录.xlsx", data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void warehousingRecordExcel(HttpServletResponse response, StorageRecordPageParam param) {
        if (ObjectUtils.isEmpty(param.getRecordType())){
            return;
        }
        QueryWrapper<StorageRecord> queryWrapper = this.queryWrapper(param);
        List<StorageRecord> storageRecords = storageRecordMapper.selectList(queryWrapper);
        try {
            List<List<Object>> rows = new ArrayList();
            ExcelData data = new ExcelData();
            data.setName("入仓记录导出");//sheet表名称
            //表头
            List<String> titles = new ArrayList();
            titles.add("订单编号");
            titles.add("入仓时间（收货时间）");
            titles.add("商品名称");
            titles.add("所属商家");
            titles.add("数量");
            titles.add("收货地址");
            titles.add("位置编码");
            titles.add("账户");
            titles.add("真实姓名");
            data.setTitles(titles);
            for (StorageRecord storageRecord : storageRecords) {
                List<Object> row1 = new ArrayList();
                row1.add(storageRecord.getOrderNo());
                if (ObjectUtils.isNotEmpty(storageRecord.getInTime())){
                    String format = DateUtils.yMdHmsS.format(storageRecord.getInTime());
                    row1.add(format);
                }else {
                    row1.add("");
                }
                row1.add(storageRecord.getGoodsName());
                row1.add(storageRecord.getShopName()+"("+"商家id:"+storageRecord.getShopId()+")");
                row1.add(storageRecord.getCount());
                row1.add(storageRecord.getReceiptAddress());
                row1.add(storageRecord.getAddressCode());
                row1.add(storageRecord.getAccount());
                row1.add(storageRecord.getRealName());
                rows.add(row1);
            }
            data.setRows(rows);
            ExportExcelUtils.exportExcel(response, "入仓记录.xlsx", data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
